package com.xh.one2one.lesson.controller;

import com.xh.one2one.lesson.service.ILessonSummaryDetailsService;
import com.xh.one2one.lesson.model.LessonSummaryDetails;
import com.xh.one2one.lesson.bean.request.LessonSummaryDetailsRequest;
import com.xh.core.bean.XhExportHeaderBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import org.springframework.validation.annotation.Validated;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.xh.core.controller.BaseController;

/**
 * <p>
 * 教学总结详情 前端控制器
 * </p>
 *
 * @author TW
 * @since 2021-05-06
 */
@RestController
@RequestMapping("/admin/lessonSummaryDetails")
public class LessonSummaryDetailsController extends BaseController {

    @Autowired
    private ILessonSummaryDetailsService lessonSummaryDetailsService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryPage")
    public AjaxResult queryPage(XhPage page, LessonSummaryDetailsRequest bean){
        page=lessonSummaryDetailsService.queryLessonSummaryDetailsPage(page,bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",page);
    }


    /**
     * 分页错题数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryWrongQuestionPage")
    public AjaxResult queryWrongQuestionPage(@RequestBody LessonSummaryDetailsRequest bean){
        XhPage page = new XhPage();
        page.setCurrent(bean.getCurrent());
        page.setSize(bean.getSize());
        if(CollectionUtils.isEmpty(bean.getLessonIds()) && CollectionUtils.isEmpty(bean.getStudentIds())){
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"参数错误！",page);
        }
        page=lessonSummaryDetailsService.queryWrongQuestionPage(page,bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",page);
    }

    /**
     * 错题学生数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryWrongStudentGroupByLessonId")
    public AjaxResult queryWrongStudentGroupByLessonId(@RequestBody LessonSummaryDetailsRequest bean){

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",lessonSummaryDetailsService.queryWrongStudentGroupByLessonId(bean));
    }
    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/query")
    public AjaxResult query(LessonSummaryDetailsRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",lessonSummaryDetailsService.queryLessonSummaryDetails(bean));
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/querySummaryQuestion")
    public AjaxResult querySummaryQuestion(LessonSummaryDetailsRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",lessonSummaryDetailsService.querySummaryQuestion(bean));
    }


    @PostMapping("/querySummaryDetailDtoQuestion")
    public AjaxResult querySummaryDetailDtoQuestion(LessonSummaryDetailsRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",lessonSummaryDetailsService.querySummaryDetailDtoQuestion(bean));
    }
    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryWrongCount")
    public AjaxResult queryWrongCount(LessonSummaryDetailsRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",lessonSummaryDetailsService.queryWrongCount(bean));
    }

    /**
     * 小程序查询有错题的学科数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/querySubjectForWrong")
    public AjaxResult querySubjectForWrong(LessonSummaryDetailsRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",lessonSummaryDetailsService.querySubjectForWrong(bean));
    }

    /**
     * 导出全部
     * @param bean
     * @return
     */
    @PostMapping(value="/exportAll")
    public AjaxResult exportAll(LessonSummaryDetailsRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=lessonSummaryDetailsService.exportAll(request,exportHeaderBeans,bean,"queryLessonSummaryDetailsPage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     * @param lessonSummaryDetails 实体对象
     * @return
     */
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated LessonSummaryDetails lessonSummaryDetails){
        if(lessonSummaryDetails.getId()==null){
            lessonSummaryDetailsService.insert(lessonSummaryDetails);
        }else{
            lessonSummaryDetailsService.update(lessonSummaryDetails);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量保存
     * @param beans 实体对象
     * @return
     */
    @PostMapping(value="/batchSave")
    public AjaxResult saveOrUpdate(@Validated @RequestBody  List<LessonSummaryDetailsRequest> beans){
        if(CollectionUtils.isEmpty(beans)){
            return new AjaxResult(AjaxResultConstant.Result.ERROR,"参数错误！");
        }
        lessonSummaryDetailsService.batchSave(beans);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,beans);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        lessonSummaryDetailsService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        lessonSummaryDetailsService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }


}

